import { SalesService } from 'src/app/@core/services/sales/sales.service';
import { ProcessService } from './../../../@core/services/process/process.service';
import { MaterialService } from './../../../@core/services/material/material.service';
import { MomService } from 'src/app/@core/services/mom/mom.service';
import { Component, Input } from '@angular/core';
import { environment } from 'src/environments/environment';
@Component({
  selector: 'app-attachment',
  templateUrl: './attachment.component.html',
  styleUrls: ['./attachment.component.scss']
})
export class AttachmentComponent {
  @Input() data: any;
  selectedFiles: { url: string, name: string, type: string }[] = [];
  hovering = {};
  apiUrl = environment.modifiedApiUrl;
  urls=''
  constructor(private MomService:MomService,private MaterialService:MaterialService,private ProcessService:ProcessService,private SalesService:SalesService) { }
  ngOnInit(): void {
   if(this.data.urls){
    if (typeof this.data.urls === 'string'){
      this.selectedFiles = this.data.urls.split(',').map((url: string) => {
        const fullUrl = this.apiUrl + url;
        const name = url.split('_').pop(); // 从 URL 中获取文件名
        return { url: fullUrl, name: name, type: '' };
      });
    }
     if (Array.isArray(this.data.urls)) {
      this.selectedFiles = this.data.urls.map((url: string) => {
        const fullUrl = this.apiUrl + url;
        const name = url.split('_').pop(); // 从 URL 中获取文件名
        return { url: fullUrl, name: name, type: '' };
      });
    }
   }
  }
  handleFileInput(event: any) {
    const file = event.target.files[0];
    if (file) {
      if(this.data.data == 'MOM附件'){
        this.MomService.UploadMOMAttachment(file).subscribe((res:any)=>{
          this.selectedFiles.push({ url: res.attachmentUrl, name: file.name, type: file.type });
          this.urls = this.selectedFiles.map(file => file.url.replace(this.apiUrl, '')).join(',');
        },error=>{
        }
      )
      }else if(this.data.data == '工单附件'){
        this.MomService.UploadMOMAttachment(file).subscribe((res:any)=>{
          this.selectedFiles.push({ url: res.attachmentUrl, name: file.name, type: file.type });
          this.urls = this.selectedFiles.map(file => file.url.replace(this.apiUrl, '')).join(',');
        },error=>{
        }
      )
      }else if(this.data.data == '物料附件'){
        this.MaterialService.uploadAttachment(file).subscribe((res:any)=>{
          this.selectedFiles.push({ url: res.attachmentUrl, name: file.name, type: file.type });
          this.urls = this.selectedFiles.map(file => file.url.replace(this.apiUrl, '')).join(',');
        },error=>{
        }
        )
      }else if(this.data.data == '工序附件'){
        this.ProcessService.UploadAttachment(file).subscribe((res:any)=>{
          this.selectedFiles.push({ url: res.attachmentUrl, name: file.name, type: file.type });
          this.urls = this.selectedFiles.map(file => file.url.replace(this.apiUrl, '')).join(',');
        })
      }else if(this.data.data=='销售附件'){
        this.SalesService.UploadAttachment(file).subscribe((res:any)=>{
          this.selectedFiles.push({ url: res.attachmentUrl, name: file.name, type: file.type });
          this.urls = this.selectedFiles.map(file => file.url.replace(this.apiUrl, '')).join(',');
        },error=>{
        }
      )
      }
    }
  }
  removeFile(index: number) {
    this.selectedFiles.splice(index, 1);
  }
}
